package GUI;

import java.awt.Rectangle;
import java.awt.TextArea;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Client_logic.DealSale;
import Client_logic.ClientNotInitializesException;
import Client_logic.ConnectionToServer;
import Client_logic.Product;

/**
 * This class use for submits selsmen to clients.
 * @author ido
 * @author michael
 * @author matan
 * @author sharon
 */
public class ScheduleForm extends JPanel
{
	private JLabel clientsjLabel = null;
	private JComboBox clientsjComboBox = null;
	private JLabel selsmenjLabel = null;
	private JComboBox selsmenjComboBox = null;
	private JCheckBox autojCheckBox = null;
	private JLabel numofclientjLabel = null;
	private JTextField numofclientjTextField = null;
	private JLabel totalclientsjLabel = null;
	private JTextField totalclientsjTextField = null;
	private JLabel totalselsmenjLabel = null;
	private JTextField totalselmensjTextField = null;
	private TextArea submitetextArea = null;
	private JLabel submitedjLabel = null;
	private JTextField submitedjTextField = null;
	private JLabel leftjLabel = null;
	private JTextField leftjTextField = null;
	private JButton submitjButton = null;
	private JButton finishjButton = null;
	private JButton backjButton = null;
	private JButton canceljButton = null;
	private DealSale dealsale = null;
	private boolean isdealsaleSave;
	
	/**
	 * This is a default constructor 
	 */
	public ScheduleForm(DealSale dealsale ) 
	{
		super();
		this.dealsale = dealsale;	
		isdealsaleSave = false;
		initialize();
	}

	/**
	 * This method initializes ScheduleForm
	 */
	private void initialize() 
	{
		leftjLabel = new JLabel();
		leftjLabel.setBounds(new Rectangle(150, 285, 48, 22));
		leftjLabel.setText("Left:");
		
		submitedjLabel = new JLabel();
		submitedjLabel.setBounds(new Rectangle(16, 285, 65, 22));
		submitedjLabel.setText("Submited:");
		
		totalselsmenjLabel = new JLabel();
		totalselsmenjLabel.setBounds(new Rectangle(286, 60, 125, 22));
		totalselsmenjLabel.setText("Number of selsmens:");
		
		totalclientsjLabel = new JLabel();
		totalclientsjLabel.setBounds(new Rectangle(17, 59, 107, 22));
		totalclientsjLabel.setText("Number of client:");
		
		numofclientjLabel = new JLabel();
		numofclientjLabel.setBounds(new Rectangle(16, 124, 171, 22));
		numofclientjLabel.setText("Number of client per selmen:");
		
		selsmenjLabel = new JLabel();
		selsmenjLabel.setBounds(new Rectangle(285, 20, 107, 22));
		selsmenjLabel.setText("Selsmens:");
		
		clientsjLabel = new JLabel();
		clientsjLabel.setBounds(new Rectangle(17, 20, 107, 22));
		clientsjLabel.setText("Clients:");
		
		
		this.setLayout(null);
		this.setSize(550, 420);
		this.add(clientsjLabel, null);
		this.add(getClientsjComboBox(), null);
		this.add(selsmenjLabel, null);
		this.add(getSelsmenjComboBox(), null);
		this.add(getAutojCheckBox(), null);
		this.add(numofclientjLabel, null);
		this.add(getNumofclientjTextField(), null);
		this.add(totalclientsjLabel, null);
		this.add(getTotalclientsjTextField(), null);
		this.add(totalselsmenjLabel, null);
		this.add(getTotalselmensjTextField(), null);
		this.add(getSubmitetextArea(), null);
		this.add(submitedjLabel, null);
		this.add(getSubmitedjTextField(), null);
		this.add(leftjLabel, null);
		this.add(getLeftjTextField(), null);
		this.add(getSubmitjButton(), null);
		this.add(getFinishjButton(), null);		
		getClientsjComboBox();
		getSelsmenjComboBox();
	}
	
	/**
	 * @return the dealsale
	 */
	public DealSale getDealSale() {
		return dealsale;
	}

	/**
	 * @param dealsale the dealsale to set
	 */
	public void setDealSale(DealSale dealsale) {
		this.dealsale = dealsale;
	}

	/**
	 * This method initializes clientsjComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getClientsjComboBox() {
		if (clientsjComboBox == null) {
			clientsjComboBox = new JComboBox();
			clientsjComboBox.setBounds(new Rectangle(124, 20, 107, 22));			
		}
		return clientsjComboBox;
	}
	
	/**
	 * This method load all clients at sector to clientsjComboBox
	 */
	public void updateClientsjComboBox()
	{
		long[] clientlist = null;
		try
		{
			int i=0,itemcount = clientsjComboBox.getItemCount();
			if (itemcount > 0) 
			{
				clientsjComboBox.addItem(0);
				clientsjComboBox.setSelectedItem(0);				
			}
			for (i=0; i < itemcount ; i++ )
			{				
				clientsjComboBox.removeItemAt(0);						
			}			
			ConnectionToServer connectionToserver = new ConnectionToServer();
			clientlist = (long[])connectionToserver.loadAllclientAtSector(Integer.parseInt(dealsale.getTemplate().getClientmarket()));			
			if (clientlist != null)
			{
				int len = clientlist.length;
				for(i=0; i < len; i++)
				{
					clientsjComboBox.addItem(clientlist[i]);							
				}
				totalclientsjTextField.setText(String.valueOf(len));
			}	
		}
		catch(ClientNotInitializesException exc)
		{
			JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
		}
		catch(Exception except)
		{
			JOptionPane.showMessageDialog(null, "SYSTEM ERROR!!!","",JOptionPane.ERROR_MESSAGE);
		}				
	}

	/**
	 * This method load all sells men to selsmenjComboBox
	 */
	public void updateSelsmenjComboBox()
	{
		long[] sellsmenlist = null;
		try
		{
			int i=0,itemcount = selsmenjComboBox.getItemCount();
			if (itemcount > 0) 
			{
				selsmenjComboBox.addItem(0);
				selsmenjComboBox.setSelectedItem(0);				
			}
			for (i=0; i < itemcount ; i++ )
			{				
				selsmenjComboBox.removeItemAt(0);						
			}			
			ConnectionToServer connectionToserver = new ConnectionToServer();
			sellsmenlist = (long[])connectionToserver.loadAllSellsMem();			
			if (sellsmenlist != null)
			{
				int len = sellsmenlist.length;
				for(i=0; i < len; i++)
				{
					selsmenjComboBox.addItem(sellsmenlist[i]);							
				}
				totalselmensjTextField.setText(String.valueOf(len));
			}	
		}
		catch(ClientNotInitializesException exc)
		{
			JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
		}
		catch(Exception except)
		{
			JOptionPane.showMessageDialog(null, "SYSTEM ERROR!!!","",JOptionPane.ERROR_MESSAGE);
		}		
	}
	
	/**
	 * This method initializes selsmenjComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getSelsmenjComboBox() {
		if (selsmenjComboBox == null) {
			selsmenjComboBox = new JComboBox();
			selsmenjComboBox.setBounds(new Rectangle(392, 20, 107, 22));			
		}
		return selsmenjComboBox;
	}

	/**
	 * This method initializes autojCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getAutojCheckBox()
	{
		if (autojCheckBox == null) 
		{
			autojCheckBox = new JCheckBox();
			autojCheckBox.setBounds(new Rectangle(16, 89, 54, 22));
			autojCheckBox.setText("Auto");
			autojCheckBox.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if (autojCheckBox.isSelected())
						numofclientjTextField.setEditable(true);
					else
						numofclientjTextField.setEditable(false);
				}
			});	
		}
		return autojCheckBox;
	}

	/**
	 * This method initializes numofclientjTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getNumofclientjTextField() 
	{
		if (numofclientjTextField == null) 
		{
			numofclientjTextField = new JTextField();
			numofclientjTextField.setBounds(new Rectangle(187, 124, 39, 22));
			numofclientjTextField.setEditable(false);
		}
		return numofclientjTextField;
	}

	/**
	 * This method initializes totalclientsjTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTotalclientsjTextField() 
	{
		if (totalclientsjTextField == null) 
		{
			totalclientsjTextField = new JTextField();
			totalclientsjTextField.setBounds(new Rectangle(124, 59, 107, 22));
			totalclientsjTextField.setEditable(false);
		}
		return totalclientsjTextField;
	}

	/**
	 * This method initializes totalselmensjTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTotalselmensjTextField() 
	{
		if (totalselmensjTextField == null) 
		{
			totalselmensjTextField = new JTextField();
			totalselmensjTextField.setBounds(new Rectangle(411, 60, 88, 22));
			totalselmensjTextField.setEditable(false);
		}
		return totalselmensjTextField;
	}

	/**
	 * This method initializes submitetextArea	
	 * 	
	 * @return java.awt.TextArea	
	 */
	private TextArea getSubmitetextArea()
	{
		if (submitetextArea == null) 
		{
			submitetextArea = new TextArea();
			submitetextArea.setBounds(new Rectangle(120, 165, 290, 105));
			submitetextArea.setEditable(false);
		}
		return submitetextArea;
	}

	/**
	 * This method initializes submitedjTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getSubmitedjTextField() {
		if (submitedjTextField == null) {
			submitedjTextField = new JTextField();
			submitedjTextField.setBounds(new Rectangle(82, 285, 55, 22));
			submitedjTextField.setEditable(false);
		}
		return submitedjTextField;
	}

	/**
	 * This method initializes leftjTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getLeftjTextField() {
		if (leftjTextField == null) {
			leftjTextField = new JTextField();
			leftjTextField.setBounds(new Rectangle(197, 285, 61, 22));
			leftjTextField.setEditable(false);
		}
		return leftjTextField;
	}

	/**
	 * This method initializes submitjButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getSubmitjButton() 
	{
		if (submitjButton == null) 
		{
			submitjButton = new JButton();
			submitjButton.setBounds(new Rectangle(250, 330, 96, 22));
			submitjButton.setText("submit");
			submitjButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					long cliet=0, sellsmen=0;
					String str = "";
					int i, j, numofsellsmen, numpersellsmen;
					if (autojCheckBox.isSelected())
					{
						try
						{
							numpersellsmen = Integer.parseInt(numofclientjTextField.getText());
						}
						catch(NumberFormatException numexc)
						{
							JOptionPane.showMessageDialog(null, "ILEGAL INPUT","",JOptionPane.WARNING_MESSAGE);
							return;
						}						
						numofsellsmen = selsmenjComboBox.getItemCount();
						for(i=0; i < numofsellsmen; i++ )
						{
							sellsmen = (Long)selsmenjComboBox.getItemAt(i);
							for (j=0; j < numpersellsmen; j++)
							{
								if (clientsjComboBox.getItemCount()==0)
									return;
								str = submitetextArea.getText();
								cliet = (Long)clientsjComboBox.getItemAt(0);
								if (sellsmen == 0 || cliet == 0)
									return;
								str = str+sellsmen+":"+cliet+".\n";
								submitetextArea.setText(str);
								clientsjComboBox.removeItem(cliet);
								leftjTextField.setText(String.valueOf(clientsjComboBox.getItemCount()));
							}
						}						
					}
					else
					{
						if (clientsjComboBox.getItemCount()==0)
							return;
						sellsmen = (Long)selsmenjComboBox.getSelectedItem();
						cliet = (Long)clientsjComboBox.getSelectedItem();
						if (sellsmen == 0 || cliet == 0)
							return;
						str = submitetextArea.getText();
						str = str+sellsmen+":"+cliet+".\n";
						submitetextArea.setText(str);
						clientsjComboBox.removeItem(cliet);
						leftjTextField.setText(String.valueOf(clientsjComboBox.getItemCount()));
						totalclientsjTextField.setText(String.valueOf(clientsjComboBox.getItemCount()));
						leftjTextField.setText(String.valueOf(clientsjComboBox.getItemCount()));
					}
				}
			});	
			
		}
		return submitjButton;
	}

	/**
	 * This method initializes finishjButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getFinishjButton() 
	{
		if (finishjButton == null)
		{
			finishjButton = new JButton();
			finishjButton.setBounds(new Rectangle(377, 330, 90, 22));
			finishjButton.setText("finish");
			finishjButton.addActionListener(new java.awt.event.ActionListener() 
			{
				String isave, msg;
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if (clientsjComboBox.getItemCount() == 0 || 
							((clientsjComboBox.getItemCount() == 1) && 
									(Long)clientsjComboBox.getItemAt(0) == 0))
					{
						try 
						{
							if (!isdealsaleSave)
							{
								isave = dealsale.saveDealSale();
								if (isave != null)
								{
									if (isave.equals("OK"))
									{
										isdealsaleSave = true;
									}
									else
									{
										JOptionPane.showMessageDialog(null, "Campaign not saved","",JOptionPane.WARNING_MESSAGE);
										return;
									}
								}
							}							
							ConnectionToServer connectiontoserver = new ConnectionToServer();
							msg = (String)connectiontoserver.updateSellsMenSchedule(submitetextArea.getText(), dealsale.getDealSaleID());
							if (msg != null)
							{
								if (msg.equals("OK"))
									JOptionPane.showMessageDialog(null, "Campaign and Schedule succcessfuly saved");
								else
									JOptionPane.showMessageDialog(null, "Schedule not saved");							
							}
							else
								JOptionPane.showMessageDialog(null, "ERROR!!","",JOptionPane.ERROR_MESSAGE);
						} catch (ClientNotInitializesException e1) 
						{
							JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
						}
					}
					else
					{
						JOptionPane.showMessageDialog(null, "Not all clients submited","",JOptionPane.WARNING_MESSAGE);
					}
				}
			});		
		}
		return finishjButton;
	}
}
